package com.work.biz.service.impl;

import java.util.List;

import com.work.common.utils.DateUtils;
import com.work.sys.domain.SysUserRole;
import com.work.sys.mapper.SysUserMapper;
import com.work.sys.mapper.SysUserRoleMapper;
import com.work.sys.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.work.biz.mapper.BizCounselorMapper;
import com.work.biz.domain.BizCounselor;
import com.work.biz.service.IBizCounselorService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 辅导员Service业务层处理
 *
 * @author lee
 * @date 2021-04-22
 */
@Service
public class BizCounselorServiceImpl implements IBizCounselorService {
    @Autowired
    private BizCounselorMapper bizCounselorMapper;
    @Autowired
    private SysUserMapper userInfoMapper;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询辅导员
     *
     * @param counselorId 辅导员ID
     * @return 辅导员
     */
    @Override
    public BizCounselor selectBizCounselorById(Long counselorId) {
        return bizCounselorMapper.selectBizCounselorById(counselorId);
    }

    /**
     * 查询辅导员列表
     *
     * @param bizCounselor 辅导员
     * @return 辅导员
     */
    @Override
    public List<BizCounselor> selectBizCounselorList(BizCounselor bizCounselor) {
        return bizCounselorMapper.selectBizCounselorList(bizCounselor);
    }

    /**
     * 新增辅导员
     *
     * @param bizCounselor 辅导员
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBizCounselor(BizCounselor bizCounselor) {
        bizCounselor.setCreateTime(bizCounselor.getCreateTime() == null ? DateUtils.getNowDate() : bizCounselor.getCreateTime());
        if(bizCounselor.getCreateBy() == null) {
            String createBy = userInfoMapper.selectSysUserById(bizCounselor.getCounselorId()).getNickName();
            bizCounselor.setCreateBy(createBy);
        }
        SysUserRole userRole = new SysUserRole();
        userRole.setRoleId(2L);
        userRole.setUserId(bizCounselor.getUserId());
        List<SysUserRole> userRoles = userRoleMapper.selectUserRoleList(userRole);
        if (userRoles.size() <= 0) {
            roleService.insertUserRole(userRole);
        }
        return bizCounselorMapper.insertBizCounselor(bizCounselor);
    }

    /**
     * 修改辅导员
     *
     * @param bizCounselor 辅导员
     * @return 结果
     */
    @Override
    public int updateBizCounselor(BizCounselor bizCounselor) {
        bizCounselor.setUpdateTime(DateUtils.getNowDate());
        return bizCounselorMapper.updateBizCounselor(bizCounselor);
    }

    /**
     * 批量删除辅导员
     *
     * @param counselorIds 需要删除的辅导员ID
     * @return 结果
     */
    @Override
    public int deleteBizCounselorByIds(Long[] counselorIds) {
        return bizCounselorMapper.deleteBizCounselorByIds(counselorIds);
    }

    /**
     * 删除辅导员信息
     *
     * @param counselorId 辅导员ID
     * @return 结果
     */
    @Override
    public int deleteBizCounselorById(Long counselorId) {
        return bizCounselorMapper.deleteBizCounselorById(counselorId);
    }
}
